using System;
using System.Collections.Generic;
using Twins.Infrastructure;
using Twins.Interface;

namespace Twins.Classes
{
    public class HashsLineCodeMethod : IHashsLineCodeMethod
    {
        private readonly List<ILineCode> hashsLineCode = new List<ILineCode>();

        public void AddLineCode(ILineCode lineCode)
        {
            if (lineCode == null)
                throw new ArgumentNullException();
            hashsLineCode.Add(lineCode);
        }

        public List<ILineCode> HashsLineCode
        {
            get { return hashsLineCode; }
        }

        public IEnumerable<ILineCode> DuplicationHashsLineCode
        {
            get
            {
                var  duplicationHashsLineCode = new List<ILineCode>();
                var adderLineCode = IoC.Resolve<IAdderLineCode>();
                if (hashsLineCode.Count>=4)
                for (int currentNumber = 0; currentNumber < hashsLineCode.Count-2; currentNumber++)
                for (int nextNumber = currentNumber + 2; nextNumber < hashsLineCode.Count-1; nextNumber++)
                    if (hashsLineCode[currentNumber].HashLineCode == hashsLineCode[nextNumber].HashLineCode)
                    {
                        if (hashsLineCode[currentNumber+1].HashLineCode == hashsLineCode[nextNumber+1].HashLineCode)
                        {
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCode[currentNumber]);
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCode[nextNumber]);
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCode[currentNumber+1]);
                            adderLineCode.AddLineCode(duplicationHashsLineCode, hashsLineCode[nextNumber+1]);
                        }
                    }
                return duplicationHashsLineCode;
            }
        }
    }
}